package com.yy.micro.magic.data.rest;

import com.yy.micro.magic.data.domain.MagicRuleVo;
import com.yy.micro.magic.data.domain.MagicPlanScheduleVo;
import com.yy.micro.magic.data.resource.PlanSchemeResource;
import com.yy.micro.magic.data.service.rule.MagicRuleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("magic-cube/data")
public class MagicCubeDataRestful {

    @Autowired
    private MagicRuleService magicRuleService;

    @Autowired
    private PlanSchemeResource schemeResource;

    @PostMapping("/rule/save")
    public MagicRuleVo save(@RequestBody MagicRuleVo rule){

        return magicRuleService.save(rule);
    }

    @GetMapping("/rule/load/{ruleId}")
    public MagicRuleVo load(@PathVariable  Long ruleId){
        return magicRuleService.load(ruleId);
    }

    @PostMapping("/plan/save")
    public MagicPlanScheduleVo save(@RequestBody MagicPlanScheduleVo scheme){
        return schemeResource.update(scheme);
    }

    @GetMapping("/plan/load/{planId}")
    public MagicPlanScheduleVo loadScheme(@PathVariable Long planId){
        return schemeResource.load(planId);
    }

    @PostMapping("plan/{planId}/{ruleId}")
    public MagicPlanScheduleVo addPlanRule(@PathVariable Long planId, @PathVariable Long ruleId){
        MagicPlanScheduleVo vo = schemeResource.load(planId);
        vo.addRule(ruleId);
        return schemeResource.update(vo);
    }
    @PostMapping("plan/{planId}")
    public MagicPlanScheduleVo addPlanRules(@PathVariable Long planId, @RequestBody Set<Long> ruleIds){
        MagicPlanScheduleVo vo = schemeResource.load(planId);
        vo.setRuleList(ruleIds);
        return schemeResource.update(vo);
    }

    @PostMapping("plan/copy/{srcPlanCode}/{targetPlanCode}")
    public MagicPlanScheduleVo copyPlanSchedule(@PathVariable String srcPlanCode, @PathVariable String targetPlanCode){
        return  schemeResource.copyPlanSchedule(srcPlanCode,targetPlanCode);
    }

    @GetMapping("load/plancode/{planCode}")
    public MagicPlanScheduleVo findByPlanCode(@PathVariable String planCode){
        return schemeResource.findByPlanCode(planCode);
    }
}
